package com.cd826dong.clouddemo.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.cd826dong.clouddemo.service.HystrixCommandCacheService;
import com.cd826dong.clouddemo.service.HystrixCommandFailureService;
import com.netflix.hystrix.HystrixCommand.Setter;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandKey;

@RestController()
@RequestMapping("/hystrixCommandApi")
public class HystrixCommandApi {

	
	@Autowired
	@Qualifier(value = "restTemplate")
	private RestTemplate restTemplate;
	
	
	@RequestMapping(value = "/test")
	public String testName()  {
		return new HystrixCommandFailureService(restTemplate, "hystrixCommandTest").execute();
	}
	
	@RequestMapping(value = "/cacheTest/{name}")
	public String cacheTestName(@PathVariable String name)  {
		
		HystrixCommandCacheService cacheService1 = new HystrixCommandCacheService(
				Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("ExampleGroup")).
					andCommandKey(HystrixCommandKey.Factory.asKey("test")), restTemplate, name);
		String result1 =  cacheService1.execute();
		
		HystrixCommandCacheService cacheService2 = new HystrixCommandCacheService(
				Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("ExampleGroup")).
					andCommandKey(HystrixCommandKey.Factory.asKey("test")), restTemplate, name);
		String result2 =  cacheService2.execute();
		
		return String.format("result1:%s,result2:%s", result1, result2);
		
	}
}
